package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysTenant;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysTenantMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant> implements ISysTenantService {

    @Autowired
    private SysTenantMapper sysTenantMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询租户
     *
     * @param id 租户主键
     * @return 租户
     */
    @Override
    public SysTenant selectSysTenantById(Long id)
    {
        return sysTenantMapper.selectSysTenantById(id);
    }

    /**
     * 查询租户列表
     *
     * @param sysTenant 租户
     * @return 租户
     */
    @Override
    public List<SysTenant> selectSysTenantList(SysTenant sysTenant)
    {
        return sysTenantMapper.selectSysTenantList(sysTenant);
    }

    /**
     * 新增租户
     *
     * @param sysTenant 租户
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysTenant(SysTenant sysTenant)
    {
        SysUser sysUser = sysUserMapper.selectUserByUserName(sysTenant.getContactPhone());
        if(ObjectUtils.isNotEmpty(sysUser)){
            throw new ServiceException("手机号已存在,请重新录入");
        }
        sysUser =new SysUser();
        sysUser.setUserName(sysTenant.getContactPhone());
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        sysUser.setUserType("tenant");
        sysTenantMapper.insertSysTenant(sysTenant);
        sysUser.setMerchantId(sysTenant.getId());
        sysUser.setStatus("0");
        sysUser.setDelFlag("0");
        sysUser.setNickName(sysTenant.getCompanyName());
        SysUserRole sysRole = new SysUserRole();
        sysRole.setRoleId(100L);

        sysUserMapper.insertUser(sysUser);
        sysRole.setUserId(sysUser.getUserId());
        List<SysUserRole> list =new ArrayList<>();
        list.add(sysRole);
        sysUserRoleMapper.batchUserRole(list);
        return 1;
    }

    /**
     * 修改租户
     *
     * @param sysTenant 租户
     * @return 结果
     */
    @Override
    public int updateSysTenant(SysTenant sysTenant)
    {
        return sysTenantMapper.updateSysTenant(sysTenant);
    }

    /**
     * 批量删除租户
     *
     * @param ids 需要删除的租户主键
     * @return 结果
     */
    @Override
    public int deleteSysTenantByIds(Long[] ids)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除租户信息
     *
     * @param id 租户主键
     * @return 结果
     */
    @Override
    public int deleteSysTenantById(Long id)
    {
        return baseMapper.deleteSysTenantById(id);
    }

}
